package cn.edu.ctbu.gudie.restcontroller;

import cn.edu.ctbu.gudie.domain.*;
import cn.edu.ctbu.gudie.helper.PageUtils;
import cn.edu.ctbu.gudie.repository.AdviceRepository;
import cn.edu.ctbu.gudie.repository.AppointmentRepository;
import cn.edu.ctbu.gudie.repository.DepartmentsRepository;
import cn.edu.ctbu.gudie.repository.HospitalsRepository;
import cn.edu.ctbu.gudie.service.DoctorService;
import cn.edu.ctbu.gudie.service.Interface.DoctorServiceIn;
import cn.edu.ctbu.gudie.service.PatientService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping(value = "/webapi/doctor")
@Slf4j
public class DoctorRestController {
    @Autowired
    private DoctorService doctorService;
    @Autowired
    private AppointmentRepository appointmentRepository;

    /**
     * 查询全部医生信息
     *
     * @return
     */
    @GetMapping("/list")
    public ResponseEntity<?> FindAll(@RequestParam(defaultValue = "10") Integer pageSize,
                                    @RequestParam(defaultValue = "1") Integer pageNum) {
        // 校验pageNum是否小于1
        if (pageNum < 1) {
            return ResponseEntity.badRequest().body("Page number must be greater than or equal to 1");
        }
        // 校验pageSize是否小于1
        if (pageSize < 1) {
            return ResponseEntity.badRequest().body("Page size must be greater than or equal to 1");
        }
        Pageable pageable = PageRequest.of(pageNum - 1, pageSize);
        Page<Doctor> tempPatients = doctorService.findAll(pageable);
        PageUtils pageUtils = new PageUtils(tempPatients.getTotalElements(), tempPatients.getContent());
        return ResponseEntity.ok(pageUtils);
    }
    @PostMapping("/appointment")
    public ResponseEntity<?> bookAppointment(@RequestBody Appointment appointment) {
        if (appointment.getDoctorId() == null || appointment.getDoctorName() == null || appointment.getAppointmentTime() == null) {
            return ResponseEntity.badRequest().body("参数缺失");
        }

        appointment.setStatus("已预约");
        appointmentRepository.save(appointment); // 保存到数据库

        return ResponseEntity.ok("预约成功");
    }
    @GetMapping("/appointment/list")
    public ResponseEntity<?> getAllAppointments(
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestParam(defaultValue = "1") Integer pageNum) {

        // 校验pageNum是否小于1
        if (pageNum < 1) {
            return ResponseEntity.badRequest().body("Page number must be greater than or equal to 1");
        }

        // 校验pageSize是否小于1
        if (pageSize < 1) {
            return ResponseEntity.badRequest().body("Page size must be greater than or equal to 1");
        }

        // 创建Pageable对象
        Pageable pageable = PageRequest.of(pageNum - 1, pageSize);

        // 调用服务层的方法获取分页的预约信息
        Page<Appointment> tempAppointments = doctorService.getAllAppointments(pageable);

        // 使用PageUtils类来构建返回的响应
        PageUtils pageUtils = new PageUtils(tempAppointments.getTotalElements(), tempAppointments.getContent());

        return ResponseEntity.ok(pageUtils);
    }


    @DeleteMapping("/appointment/delete/{id}")
    public ResponseEntity<?> deleteAppointment(@PathVariable Integer id) {
        boolean deleted = doctorService.deleteAppointment(id);
        if (deleted) {
            return ResponseEntity.ok(Map.of("code", 1, "msg", "删除成功"));
        } else {
            return ResponseEntity.status(404).body(Map.of("code", 0, "msg", "未找到该预约"));
        }
    }
}
